Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.505 lượt xem;
- ThirdPersonNetwork.cs
- Player /
- Game /
- Scripts /
- DemoWorker /
- Demos /
- Photon Unity Networking /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class ThirdPersonNetwork : Photon.MonoBehaviour
5 {
6 ThirdPersonCamera cameraScript;
7 ThirdPersonController controllerScript;
8
9 void Awake()
10 {
11 cameraScript = GetComponent<ThirdPersonCamera>();
12 controllerScript = GetComponent<ThirdPersonController>();
13
14 if (photonView.isMine)
15 {
16 //MINE: local player, simply enable the local scripts
17 cameraScript.enabled = true;
18 controllerScript.enabled = true;
19 }
20 else
21 {
22 cameraScript.enabled = false;
23
24 controllerScript.enabled = true;
25 controllerScript.isControllable = false;
26 }
27
28 gameObject.name = gameObject.name + photonView.viewID;
29 }
30
31 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
32 {
33 if (stream.isWriting)
34 {
35 //We own this player: send the others our data
36 stream.SendNext((int)controllerScript._characterState);
37 stream.SendNext(transform.position);
38 stream.SendNext(transform.rotation);
39 }
40 else
41 {
42 //Network player, receive data
43 controllerScript._characterState = (CharacterState)(int)stream.ReceiveNext();
44 correctPlayerPos = (Vector3)stream.ReceiveNext();
45 correctPlayerRot = (Quaternion)stream.ReceiveNext();
46 }
47 }
48
49 private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
50 private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
51
52 void Update()
53 {
54 if (!photonView.isMine)
55 {
56 //Update remote player (smooth this, this looks good, at the cost of some accuracy)
57 transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
58 transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
59 }
60 }
61
62 }
2 using System.Collections;
3
4 public class ThirdPersonNetwork : Photon.MonoBehaviour
5 {
6 ThirdPersonCamera cameraScript;
7 ThirdPersonController controllerScript;
8
9 void Awake()
10 {
11 cameraScript = GetComponent<ThirdPersonCamera>();
12 controllerScript = GetComponent<ThirdPersonController>();
13
14 if (photonView.isMine)
15 {
16 //MINE: local player, simply enable the local scripts
17 cameraScript.enabled = true;
18 controllerScript.enabled = true;
19 }
20 else
21 {
22 cameraScript.enabled = false;
23
24 controllerScript.enabled = true;
25 controllerScript.isControllable = false;
26 }
27
28 gameObject.name = gameObject.name + photonView.viewID;
29 }
30
31 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
32 {
33 if (stream.isWriting)
34 {
35 //We own this player: send the others our data
36 stream.SendNext((int)controllerScript._characterState);
37 stream.SendNext(transform.position);
38 stream.SendNext(transform.rotation);
39 }
40 else
41 {
42 //Network player, receive data
43 controllerScript._characterState = (CharacterState)(int)stream.ReceiveNext();
44 correctPlayerPos = (Vector3)stream.ReceiveNext();
45 correctPlayerRot = (Quaternion)stream.ReceiveNext();
46 }
47 }
48
49 private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
50 private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
51
52 void Update()
53 {
54 if (!photonView.isMine)
55 {
56 //Update remote player (smooth this, this looks good, at the cost of some accuracy)
57 transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
58 transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
59 }
60 }
61
62 }
MINE: local player, simply enable the local scripts
We own this player: send the others our data
Network player, receive data
private Vector3 correctPlayerPos = Vector3.zero; We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; We lerp towards this
Update remote player (smooth this, this looks good, at the cost of some accuracy)